package com.hui.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hui.dto.*;
import com.hui.entity.School;
import com.hui.excel.SchoolExcel;
import com.hui.exception.IllegalParamException;
import com.hui.exception.ServerErrorException;
import com.hui.exception.VersionException;
import com.hui.mapper.SchoolMapper;
import com.hui.service.SchoolService;
import com.hui.vo.SchoolVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@CacheConfig(cacheNames = "school")
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(SchoolInsertDTO dto) {
        School school = BeanUtil.copyProperties(dto, School.class);
        if (StrUtil.isBlank(dto.getInfo())){
            school.setInfo("暂无信息");
        }
        int result = schoolMapper.insert(school);
        if (result <= 0){
            throw new ServerErrorException("DB添加失败");
        }
        return result;
    }

    @Cacheable(key = "#p0",condition = "#p0 !=null", unless = "#result == null")
    @Override
    public School select(Long id) {
        School result = schoolMapper.select(id);
        if (ObjectUtil.isNull(result)){
            throw new ServerErrorException("学校不存在");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName", unless = "#result == null")
    @Override
    public List<SchoolVO> list() {
        return schoolMapper.list(new SchoolPageDTO())
                .stream()
                .map(school -> BeanUtil.copyProperties(school, SchoolVO.class))
                .collect(Collectors.toList());
    }

    @Cacheable(key = "#root.methodName+ ':' + #p0.toString() + ':' + #p0.getPageSize()", condition = "#p0 != null")
    @Override
    public PageInfo<School> page(SchoolPageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(schoolMapper.list(dto));
    }

    @CacheEvict(allEntries = true)
    @Override
    @Retryable(value = VersionException.class)
    public int update(SchoolUpdateDTO dto) {
        Long id = dto.getId();
        if (ObjectUtil.isNull(id)){
            throw new IllegalParamException("ID为空");
        }
        School school = schoolMapper.select(id);
        if (ObjectUtil.isNull(school)){
            throw new ServerErrorException("记录不存在");
        }
        BeanUtil.copyProperties(dto, school);
        school.setUpdated(LocalDateTime.now());
        int result = schoolMapper.update(school);
        if (result <= 0){
            throw new VersionException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int delete(Long id) {
        int result = schoolMapper.delete(id);
        if (result <= 0){
            throw new ServerErrorException("DB删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int deleteBatch(List<Long> ids) {
        int result = schoolMapper.deleteBatch(ids);
        if (result <= 0){
            throw new ServerErrorException("DB删除失败");
        }
        return result;
    }
    @Override
    public List<SchoolExcel> getExcelData() {
        return schoolMapper.list(new SchoolPageDTO())
                .stream()
                .map(school -> BeanUtil.copyProperties(school, SchoolExcel.class))
                .collect(Collectors.toList());
    }


}
